package pri.black.dogview.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pri.black.dogview.domain.R;
import pri.black.dogview.domain.VideoSource;
import pri.black.dogview.service.VideoSourceService;

@RequestMapping("/video-source")
@RestController
public class VideoSourceController {

    @Autowired
    VideoSourceService videoSourceService;

    @GetMapping("/list")
    R page(Page<VideoSource> page) {
        return R.ok(videoSourceService.page(page));
    }

    @PutMapping("")
    R put(@RequestBody VideoSource videoSource) {
        videoSourceService.updateById(videoSource);
        return R.ok();
    }

    @PostMapping("")
    R post(@RequestBody VideoSource videoSource) {
        String code = RandomStringUtils.random(4, false, true);
        videoSource.setCode(code);
        videoSourceService.save(videoSource);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    R remove(@PathVariable("id") Long id) {
        videoSourceService.removeById(id);
        return R.ok();
    }
}
